﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace YJKArchStandard
{
	public class RampArchStd : IArchStdOperation
	{
		public static RampArchStd GetInstance(string sNodeName, string sFilepath)
		{
			return new RampArchStd(sNodeName, sFilepath);
		}

		private RampArchStd(string sNodeName, string sFilepath)
		{
			RampArchStd.m_listCheckData = new List<RampArchStdData>();
			this.LoadFile(sNodeName, sFilepath);
		}

		private int toInt(string str)
		{
			return Convert.ToInt32(str);
		}

		private double toDouble(string str)
		{
			return Convert.ToDouble(str);
		}

		private RampArchStdData ReadXmlData(XmlNode node)
		{
			RampArchStdData rampArchStdData = new RampArchStdData();
			int num = 0;
			rampArchStdData.m_nID = this.toInt(node.ChildNodes[num++].InnerText);
			rampArchStdData.m_sRampSlope = node.ChildNodes[num++].InnerText;
			rampArchStdData.m_nRampMaxHeight = this.toInt(node.ChildNodes[num++].InnerText);
			rampArchStdData.m_nRampHorizontalLength = this.toInt(node.ChildNodes[num++].InnerText);
			rampArchStdData.m_nRampWidth = this.toInt(node.ChildNodes[num++].InnerText);
			rampArchStdData.m_nLandHorizontalLength = this.toInt(node.ChildNodes[num++].InnerText);
			return rampArchStdData;
		}

		private XmlNode GetNodeBySpecifyStdId(string sStdId, XmlDocument xmlDoc)
		{
			XmlNode result = null;
			XmlNode documentElement = xmlDoc.DocumentElement;
			foreach (object obj in documentElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				string value = xmlNode.Attributes["id"].Value;
				if (value.Equals(sStdId))
				{
					result = xmlNode;
					break;
				}
			}
			return result;
		}

		public bool LoadFile(string sNodeName, string sFilepath)
		{
			bool result = true;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(sFilepath);
				XmlNode nodeBySpecifyStdId = this.GetNodeBySpecifyStdId("GB50763-2012", xmlDocument);
				if (nodeBySpecifyStdId != null)
				{
					for (XmlNode xmlNode = nodeBySpecifyStdId.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
					{
						RampArchStd.m_listCheckData.Add(this.ReadXmlData(xmlNode));
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		public bool WriteFile(string sNodeName, string sFilepath)
		{
			bool result = false;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(sFilepath);
				if (this.GetNodeBySpecifyStdId(sNodeName, xmlDocument) == null)
				{
					XmlNode xmlNode = xmlDocument.CreateElement("Std");
					if (xmlNode != null)
					{
						XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("id");
						xmlAttribute.Value = "GB50763-2012";
						xmlNode.Attributes.SetNamedItem(xmlAttribute);
						xmlDocument.DocumentElement.AppendChild(xmlNode);
						xmlDocument.Save(sFilepath);
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			return result;
		}

		private int GetRampID(int nRampSlope)
		{
			int result = -1;
			if (20 <= nRampSlope)
			{
				result = 6;
			}
			else if (18 <= nRampSlope && nRampSlope < 20)
			{
				result = 5;
			}
			else if (16 <= nRampSlope && nRampSlope < 18)
			{
				result = 4;
			}
			else if (12 <= nRampSlope && nRampSlope < 16)
			{
				result = 3;
			}
			else if (10 <= nRampSlope && nRampSlope < 12)
			{
				result = 2;
			}
			else if (9 == nRampSlope)
			{
				result = 1;
			}
			else if (8 >= nRampSlope)
			{
				result = 0;
			}
			return result;
		}

		private RampArchStdData GetCheckDataByIDType(int nID)
		{
			RampArchStdData result = null;
			foreach (RampArchStdData rampArchStdData in RampArchStd.m_listCheckData)
			{
				if (rampArchStdData.m_nID == nID)
				{
					result = rampArchStdData;
					break;
				}
			}
			return result;
		}

		public bool CheckRampSlope(int nRampSlope, ref string sMsg, bool bHome = false)
		{
			bool result = true;
			string text = "坡道坡度参数值不满足《无障碍设计规范》GB 50763-2012中3.4.4坡度限值不应小于1：8";
			if (nRampSlope < 8)
			{
				result = false;
				sMsg = text;
			}
			else
			{
				sMsg = "OK";
			}
			return result;
		}

		public bool CheckRampMaxHeight(int nRampSlope, int nRampMaxHeight, ref string sMsg, bool bHome = false)
		{
			bool result = true;
			string str = "坡道最大高度参数值不满足《无障碍设计规范》GB 50763-2012中3.4.4当前坡度下允许的最大高度值";
			RampArchStdData checkDataByIDType = this.GetCheckDataByIDType(this.GetRampID(nRampSlope));
			if (nRampMaxHeight > checkDataByIDType.m_nRampMaxHeight)
			{
				result = false;
				sMsg = str + checkDataByIDType.m_nRampMaxHeight.ToString("f0");
			}
			else
			{
				sMsg = "OK";
			}
			return result;
		}

		public bool CheckRampHorizontalLength(int nRampSlope, int nRampHorizontalLength, ref string sMsg, bool bHome = false)
		{
			bool result = true;
			string str = "参数值不满足《无障碍设计规范》GB 50763-2012中3.4.4当前坡度下允许的最大水平长度值";
			RampArchStdData checkDataByIDType = this.GetCheckDataByIDType(this.GetRampID(nRampSlope));
			if (nRampHorizontalLength > checkDataByIDType.m_nRampHorizontalLength)
			{
				result = false;
				sMsg = str + checkDataByIDType.m_nRampHorizontalLength.ToString("f0");
			}
			else
			{
				sMsg = "OK";
			}
			return result;
		}

		public bool CheckRampWidth(int nRampSlope, int nRampWidth, ref string sMsg, bool bHome = false)
		{
			bool result = true;
			string str = "坡道净宽度参数值不满足《无障碍设计规范》GB 50763-2012中3.4.2坡道宽度不应小于";
			RampArchStdData checkDataByIDType = this.GetCheckDataByIDType(this.GetRampID(nRampSlope));
			if (nRampWidth < checkDataByIDType.m_nRampWidth)
			{
				result = false;
				sMsg = str + checkDataByIDType.m_nRampWidth.ToString("f0");
			}
			else
			{
				sMsg = "OK";
			}
			return result;
		}

		public bool CheckLandHorizontalLength(int nRampSlope, int nLandHorizontalLength, ref string sMsg, bool bHome = false)
		{
			bool result = true;
			string str = "休息平台的水平长度参数值不满足《无障碍设计规范》GB 50763-2012中3.4.6平台水平长度不应小于";
			RampArchStdData checkDataByIDType = this.GetCheckDataByIDType(this.GetRampID(nRampSlope));
			if (nLandHorizontalLength < checkDataByIDType.m_nLandHorizontalLength)
			{
				result = false;
				sMsg = str + checkDataByIDType.m_nLandHorizontalLength.ToString("f0");
			}
			else
			{
				sMsg = "OK";
			}
			return result;
		}

		private static List<RampArchStdData> m_listCheckData;
	}
}
